<?php
namespace Admin\Controller;

use Services\Upload\OssFileService;

class CmsAdController extends AdminController{
    public function getAdCate(){
        return array(
            1=>'好友列表广告(750*300)',
            2=>'开通VIP广告图(750*300)',
        );
    }
    public function index()
    {
        $cate_list = $this->getAdCate();
        $this->meta_title = '广告管理';
        $map = array();
        $list = $this->lists('CmsAd',$map,'`is_order` Asc,id Asc',true,20);
        foreach($list as $key=>$val){
            $list[$key]['cover'] = OssFileService::getOssUrl(($val['cover']));
            $list[$key]['cate_name'] =$cate_list[$val['cate_id']];
        }
        $this->assign('list', $list);
        $this->display();
    }

    public function add(){
        $this->assign("cate_list",$this->getAdCate());
        $t_info_category = M("CmsAd");
        if(IS_AJAX){
            $cate_id = $_POST['cate_id']?trim($_POST['cate_id']):0;
            $title = $_POST['title']?trim($_POST['title']):'';
            $cover = $_POST['cover']?$_POST['cover']:'';
            $is_order = $_POST['is_order']?trim($_POST['is_order']):99;
            $url = $_POST['url']?trim($_POST['url']):'';
            $s_data['cate_id'] = $cate_id;
            $s_data['title'] = $title;
            $s_data['jump_type'] = $_POST['jump_type']?intval($_POST['jump_type']):0;
            $s_data['url'] = $url;
            $s_data['appid'] = $_POST['appid']?trim($_POST['appid']):'';
            $s_data['is_order'] = $is_order;
            $s_data['is_show'] = 1;
            $s_data['cover'] = extract_path_from_url($cover);
            $s_data['create_time'] = time();
            if($t_info_category->add($s_data)){
                $this->return_json_data(1,'广告添加成功');
            }else{
                $this->return_json_data(0,'广告添加失败，请重试');
            }
        }
        $this->meta_title = '添加广告';
        $this->display();
    }

    public function edit(){
        $this->assign("cate_list",$this->getAdCate());
        $t_info_category = M("CmsAd");
        if(IS_AJAX){
            $id = $_POST['id'];
            $cate_id = $_POST['cate_id']?trim($_POST['cate_id']):0;
            $title = $_POST['title']?trim($_POST['title']):'';
            $cover = $_POST['cover']?$_POST['cover']:'';
            $is_order = $_POST['is_order']?trim($_POST['is_order']):99;
            $url = $_POST['url']?trim($_POST['url']):'';
            $s_data['cate_id'] = $cate_id;
            $s_data['title'] = $title;
            $s_data['url'] = $url;
            $s_data['jump_type'] = $_POST['jump_type']?intval($_POST['jump_type']):0;
            $s_data['appid'] = $_POST['appid']?trim($_POST['appid']):'';
            $s_data['is_order'] = $is_order;
            $s_data['cover'] = extract_path_from_url($cover);
            $s_data['create_time'] = time();
            if($t_info_category->where("id=$id")->save($s_data)){
                $this->return_json_data(1,'广告更新成功');
            }else{
                $this->return_json_data(0,'广告更新失败，请重试');
            }
        }
        $id = $_GET['id'];
        $info = $t_info_category->find($id);
        if($info){
            $info['cover'] = OssFileService::getOssUrl(($info['cover']));
        }
        $this->assign('info',$info);
        $this->meta_title = '编辑广告';
        $this->display('add');
    }

    public function index_edit(){
        $t_info_category = M("CmsAd");
        if(IS_POST){
            $id = $_POST['id']?$_POST['id']:'';
            $is_order = $_POST['is_order']?$_POST['is_order']:99;
            $url= $_POST['url']?trim($_POST['url']):'';
            $s_data['id'] = $id;
            $s_data['url'] = $url;
            $s_data['is_order'] = $is_order;
            $s_data['create_time'] = time();
            if($t_info_category->save($s_data)){
                $this->success('更新成功');
            }else{
                $this->error('更新失败，请重试');
            }
        }
    }

    public function changeStatus(){
        $field	= I('get.c_field');
        $status	= I('get.c_value');
        $aid	= I('get.id');
        $t_info_category = M("CmsAd");
        $data['id']		= $aid;
        $data[$field]	=$status;
        if($t_info_category->save($data)){
            $this->success('更新成功');
        }else{
            $this->error('更新失败，请重试');
        }
    }

    public function del(){
        $t_info_category = M("CmsAd");
        $id = $_GET['id'];
        if($t_info_category->where("id=$id")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败，请重试');
        }
    }
}
